package com.ustadzmenjawab.indexing;

import java.net.URLEncoder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.ustadzmenjawab.shared.Constant;

public class QuerySpellCheck {
	private final static String GOOGLE_SPELL_CHECK = "http://didyoumean.info/api?tld=com&q=";
	public static String getSpellSuggestion(String query){
		if(query == null || query.length() == 0)
			return null;
		try{
			String dot = URLEncoder.encode(".", "UTF-8");
			String encUrl = GOOGLE_SPELL_CHECK+URLEncoder.encode(query, "UTF-8");
			Document doc = Jsoup.connect(encUrl).timeout(Constant.HTTP_CONNECT_TIMEOUT).get();
			if(doc == null)
				return null;
			String suggestion = doc.text();
			if(suggestion == null || suggestion.length() == 0)
				return null;
			return suggestion.replace("\\", "");
		}catch(Exception e){
			return null;
		}
		
	}
	
	public static void main(String[] args){
		String input = "masalah umat";
		System.out.println(getSpellSuggestion(input));
	}
}
